
var orientacion_c={
	distancia:.0005,
	spin:-90,
	render:function(marcador){
		if(this.visor){
			this.visor.setMap(null);
		}
		var angulo=(Math.PI/180)*(orientacion_c.spin-90);
		var centro=marcador.getPosition();
		var ang1=angulo-10;
		var ang2=angulo+10;
		var fix=(orientacion_c.distancia*(Math.sin(ang1)))+centro.lat();
		var fiy=(orientacion_c.distancia*(Math.cos(ang1)))+centro.lng();
		var ffx=(orientacion_c.distancia*(Math.sin(ang2)))+centro.lat();
		var ffy=(orientacion_c.distancia*(Math.cos(ang2)))+centro.lng();
		var coord=[
			centro,
			new google.maps.LatLng(fix, fiy),
			new google.maps.LatLng(ffx, ffy),
			];
		
		this.visor = new google.maps.Polygon({
		    paths: coord,
		    strokeColor: "#FF0000",
		    strokeOpacity: 0.8,
		    strokeWeight: 2,
		    fillColor: "#FF0000",
		    fillOpacity: 0.35
		  });
		this.visor.centro=centro;
  		this.visor.setMap(marcador.getMap());
  		this.visor.mousedown=orientacion_c.mousedown;
  		this.visor.mousemove=orientacion_c.mousemove;
  		this.visor.mouseup=orientacion_c.mouseup;
  		this.visor.movervisor=orientacion_c.movervisor;
  		
  		this.visor.movervisor(0);
  		
  		var mapa=this;
		google.maps.event.addListener(this.visor,"mousedown",function(ev){
			this.mousedown(ev);
		});
		google.maps.event.addListener(this.visor,"mouseup",function(ev){
			this.mouseup(ev);
		});
	},
	mousedown:function(ev){
		this.getMap().setOptions({draggable:false});
		
  		if(this.moving){
			google.maps.event.removeListener(this.moving);
			this.moving=false;
  		}
  		
		this.moving=google.maps.event.addListener(this,"mousemove",function(ev){
			this.mousemove(ev);
		});
		
		ev.stop();
	},
	movervisor:function(angulo){
		console.log(angulo);
		var angulo=((Math.PI*(orientacion_c.spin-angulo))/180);
		
		
		var ang1=angulo-10;
		var ang2=angulo+10;
		var fix=(orientacion_c.distancia*(Math.sin(ang1)))+this.centro.lat();
		var fiy=(orientacion_c.distancia*(Math.cos(ang1)))+this.centro.lng();
		var ffx=(orientacion_c.distancia*(Math.sin(ang2)))+this.centro.lat();
		var ffy=(orientacion_c.distancia*(Math.cos(ang2)))+this.centro.lng();
		
		
		var coord=[
			this.centro,
			new google.maps.LatLng(fix, fiy),
			new google.maps.LatLng(ffx, ffy),
			];
		this.setPaths(coord);
	},
	mousemove:function(ev){
		ev.stop();
		var ly=this.centro.lat()-ev.latLng.lat();
		var lx=this.centro.lng()-ev.latLng.lng();
		var a=Math.atan((ly)/(lx));
		
		a=Math.round((Math.abs(a)*90)/1.567);
		if(ly<=0&&lx<=0){
			a=90-a;
		}
		if(ly>=0&&lx<=0){
			a=90+a;
		}
		if(ly>=0&&lx>=0){
			a=270-a;
		}
		if(ly<=0&&lx>=0){
			a=270+a;
		}
		
		/**
		 * @TODO: Insertar aqui conexion con orientación de visor
		 */
		this.movervisor(a);
	},
	mouseup:function(ev){
		this.getMap().setOptions({draggable:true});
		google.maps.event.removeListener(this.moving);
		this.moving=false;
		ev.stop();
	}
}

mapa_interface.hooks.iniciado.push(
	function(mapa){
		mapa.orientacion={};
		mapa.orientacion_render=orientacion_c.render;
	}
);
mapa_interface.hooks.markAdded.push(
	function(mapa,marcador){
		mapa.orientacion_render(marcador);
	}
);